﻿// Inner Fire 游戏引擎库
// Lua绑定适配项目
//
// Copyright (c) 2025 by 尔西大帝. All rights reserved.
//
// 所属层级：应用层(4)
// 用于GameManager的Lua绑定适配。
//
// Author: 尔西大帝
// Email: 2670613871@qq.com
// Created: 2025-04-30

#include "lua_manager.h"
#include <game/manager/game_manager.h>
#include <game/manager/scene.h>
#include <ui/manager/ui_manager.h>

namespace ifire::game {

// C++函数：基于ObjectFactory实例执行
static int Scene_FindObject(lua_State* L) {
  auto scene = L_THIS(Scene);
  auto obj = scene->GetObjectFactory()->FindObject(L_STR(2));
  return ikit::lua_return_object(L, obj, "GameObjectMeta");
}

static int Scene_CreateObject(lua_State* L) {
  auto scene = L_THIS(Scene);
  auto obj = scene->GetObjectFactory()->CreateObject(L_STR(2));
  return ikit::lua_return_object(L, obj, "GameObjectMeta");
}

static int Scene_CreateInstancedSprite(lua_State* L) {
  auto scene = L_THIS(Scene);
  auto obj = scene->GetObjectFactory()->CreateInstancedSprite(
      L_STR(2), L_STR(3), L_STR(4), L_INT(5));
  return ikit::lua_return_object(L, obj, "InstancedSpriteObjectMeta");
}

static int Scene_CreateMapObject(lua_State* L) {
  auto scene = L_THIS(Scene);
  const char* prefab_name = L_STR(2);
  auto obj = scene->GetObjectFactory()->CreateMapObject(prefab_name);
  return ikit::lua_return_object(L, obj, "MapObjectMeta");
}

static int Scene_CreatePhysicsWorld(lua_State* L) {
  auto scene = L_THIS(Scene);
  const char* name = L_STR(2);
  auto obj = scene->CreatePhysicsWorld(name);
  return ikit::lua_return_object(L, obj, "PhysicsWorldMeta");
}

static int Scene_CreateLabel(lua_State* L) {
  auto scene = L_THIS(Scene);
  auto label =
      scene->GetUIManager()->CreateLabel(L_STR(2), L_STR(3), L_VEC2(5));
  if (label) {
    label->SetText(L_STR(4));
  }
  return ikit::lua_return_object(L, label, "LabelMeta");
}

static int Scene_CreateCamera(lua_State* L) {
  auto scene = L_THIS(Scene);
  auto camera = scene->GetObjectFactory()->CreateCamera(L_STR(2), L_STR(3));
  if (camera) {
    camera->GetTransform().SetPosition(L_VEC3(4));
  }
  return ikit::lua_return_object(L, camera, "CameraObjectMeta");
}

static int Scene_CreateButton(lua_State* L) {
  auto scene = L_THIS(Scene);

  // 创建对象
  ikit::sstr name_id = L_STR(2);
  auto button = scene->GetUIManager()->CreateButton(
      name_id, L_STR(3), L_STR(4), L_STR(5), L_VEC2(6));

  // 传入的参数ID
  int pid = static_cast<int>(luaL_optinteger(L, 8, 0));
  // 传入的参数值
  float pvalue = static_cast<float>(luaL_optnumber(L, 9, 0.0));

  // 绑定回调项目
  button->AddClickListener([scene, name_id, pid, pvalue](ui::UIComponent* btn) {
    scene->GM()->GetLua()->OnButtonClick(name_id, pid, pvalue);
  });

  return ikit::lua_return_object(L, button, "ButtonMeta");
}

static int Scene_CreateImage(lua_State* L) {
  auto scene = L_THIS(Scene);
  // 创建对象
  auto image = scene->GetUIManager()->CreateImage(
      L_STR(2), L_STR(3), L_STR(4), L_VEC2(5));

  return ikit::lua_return_object(L, image, "ImageMeta");
}

void register_lua_game_manager(lua_State* L, LuaManager* lua_manager) {

  // 注册Scene
  luaL_Reg scene_methods[] = {                                //
      {"FindObject", Scene_FindObject},                       //
      {"CreateObject", Scene_CreateObject},                   //
      {"CreateLabel", Scene_CreateLabel},                     //
      {"CreateButton", Scene_CreateButton},                   //
      {"CreateImage", Scene_CreateImage},                   //
      {"CreateCamera", Scene_CreateCamera},                   //
      {"CreateInstancedSprite", Scene_CreateInstancedSprite}, //
      {"CreateMapObject", Scene_CreateMapObject},             //
      {"CreatePhysicsWorld", Scene_CreatePhysicsWorld},       //

      {NULL, NULL}};
  lua_register_metatable(L, "SceneMeta", scene_methods);
  lua_set_global_object(
      L, lua_manager->GM()->CurrentScene(), "Scene", "SceneMeta");
}

} // namespace ifire::game